﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using System.Data;

namespace PremiereMX
{
    public class SupplierLogic
    {
        SupplierDTO m_subject = new SupplierDTO();
        IMainForm m_mainForm;
        IUserControlSupplier m_form;
        EditMode m_editMode = EditMode.OK;

        IList m_tops;
        IList m_employees;
        IList m_taxs;
        IList m_currencies;
        IList m_customerCategory1List;
        IList m_customerCategory2List;
        IList m_customerCategory3List;
        IList m_priceCategoryList;

        public SupplierLogic(IMainForm mainForm, IUserControlSupplier form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
            updateSupplierList();
            loadList();
        }
        private void loadList()
        {
            m_tops = ServiceRegistry.GetInstance().GetTOPService().LoadAll();
            m_employees = ServiceRegistry.GetInstance().GetEmployeeMasterService().FindAllIsSalesmanEmployee();
            m_taxs = ServiceRegistry.GetInstance().GetTaxService().LoadAll();
            m_currencies = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            m_customerCategory1List = ServiceRegistry.GetInstance().GetSupplierCategoryService().FindAllSupplierCategory1();
            m_customerCategory2List = ServiceRegistry.GetInstance().GetSupplierCategoryService().FindAllSupplierCategory2();
            m_customerCategory3List = ServiceRegistry.GetInstance().GetSupplierCategoryService().FindAllSupplierCategory3();
            m_priceCategoryList = ServiceRegistry.GetInstance().GetPriceCategoryService().LoadAll();
        }
        public IList TaxList
        {
            get { return m_taxs; }
        }
        public IList EmployeeList
        {
            get { return m_employees; }
        }
        public IList TOPLost
        {
            get { return m_tops; }
        }
        public IList CurrencyList
        {
            get { return m_currencies; }
        }
        public IList SupplierCategory1List
        {
            get { return m_customerCategory1List; }
        }
        public IList SupplierCategory2List
        {
            get { return m_customerCategory2List; }
        }
        public IList SupplierCategory3List
        {
            get { return m_customerCategory3List; }
        }
        public IList PriceCategoryList
        {
            get { return m_priceCategoryList; }
        }
        private void updateForm()
        {
            m_form.Code = m_subject.Code;
            m_form.Named = m_subject.Name;
            m_form.LastUpdate = m_subject.LastUpdate;
            m_form.ComputerName = m_subject.ComputerName;
            m_form.UserUpdateBy = m_subject.UserName;
            m_form.Address = m_subject.Address;
            m_form.City = m_subject.City;
            m_form.Contact = m_subject.Contact;
            m_form.Country = m_subject.Country;
            m_form.Currency = m_subject.Currency;
            m_form.Email = m_subject.Email;
            m_form.Fax = m_subject.Fax;
            m_form.Phone = m_subject.Phone;
            m_form.Province = m_subject.Province;
            m_form.Employee = m_subject.Employee;
            m_form.Tax = m_subject.Tax;
            m_form.TaxNo = m_subject.TaxNo;
            m_form.TOP = m_subject.TOP;
            m_form.Website = m_subject.Website;
            m_form.Zipcode = m_subject.ZipCode;
            m_form.Note = m_subject.Note;
           // m_form.CreditLimit = m_subject.CreditLimit;
            m_form.SupplierCategory1 = m_subject.SupplierCategory1;
            m_form.SupplierCategory2 = m_subject.SupplierCategory2;
            m_form.SupplierCategory3 = m_subject.SupplierCategory3;
           // m_form.PriceCategory = m_subject.PriceCategory;
            m_form.VendorType = m_subject.VendorType;
            m_form.Barcode = m_subject.Barcode;
            m_form.Discontinue = m_subject.Discontinue;
            IList activities = ServiceRegistry.GetInstance().GetSupplierService().FindEventByVendor(m_subject.Id);
            m_form.UpdateActivity(activities);
        }
        private void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        private void updateSupplierList()
        {
            IList list = ServiceRegistry.GetInstance().GetSupplierService().LoadAll();
            m_form.UpdateList(list);
        }
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        private void updateSubject()
        {
            m_subject.Code = m_form.Code;
            m_subject.ComputerName = Environment.MachineName;
            m_subject.Name = m_form.Named;
            m_subject.Note = m_form.Note;

            m_subject.Address = m_form.Address;
            m_subject.City = m_form.City;
            m_subject.Contact = m_form.Contact;
            m_subject.Country = m_form.Country;
            m_subject.Currency = m_form.Currency;
            m_subject.Email = m_form.Email;
            m_subject.Fax = m_form.Fax;
            m_subject.Phone = m_form.Phone;
            m_subject.Province = m_form.Province;
            m_subject.Employee = m_form.Employee;
            m_subject.Tax = m_form.Tax;
            m_subject.TaxNo = m_form.TaxNo;
            m_subject.TOP = m_form.TOP;
            m_subject.Website = m_form.Website;
            m_subject.ZipCode = m_form.Zipcode;

           // m_subject.CreditLimit = m_form.CreditLimit;
            m_subject.SupplierCategory1 = m_form.SupplierCategory1;
            m_subject.SupplierCategory2 = m_form.SupplierCategory2;
            m_subject.SupplierCategory3 = m_form.SupplierCategory3;
           // m_subject.PriceCategory = m_form.PriceCategory;
            m_subject.VendorType = m_form.VendorType;
            m_subject.Barcode = m_form.Barcode;
            m_subject.Discontinue = m_form.Discontinue;

            m_subject.UserName = m_mainForm.GetUser().Code;       
            m_subject.LastUpdate = DateTime.Now;
        }
        internal void UpdateSubject(SupplierDTO curr)
        {
            m_subject = curr;
            m_form.ClearForm();
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
        }
        public void Save()
        {
            try
            {
                if (m_form.IsValidSubject())
                {
                    updateSubject();
                    m_subject.Id = ServiceRegistry.GetInstance().GetSupplierService().Save(m_subject);
                    m_form.ShowMessage("Record has been saved!");
                    m_form.SetToReadOnly(true);
                    SetButtonEditMode(EditMode.OK);
                    m_form.UpdateList(new object[] { m_subject });
                }
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
        internal void Delete()
        {
            if (m_subject.Id == 0) return;
            ServiceRegistry.GetInstance().GetSupplierService().Delete(m_subject);
            m_form.RemoveItem(m_subject.Id);
            m_form.ClearForm();
        }
        internal void New()
        {
            m_subject = new SupplierDTO();
            m_form.ClearForm();
            SetButtonEditMode(EditMode.Insert);
            m_form.SetToReadOnly(false);
            m_form.FocusToCode();
        }
        public void Refresh()
        {
        }
        internal void Print()
        {
            DataSet ds = ServiceRegistry.GetInstance().GetSupplierService().GetPreviewReportData(m_subject.Id);
            m_mainForm.Print(ds);
        }
    }
}
